package book.chapter05.$5_3_7;

import book.util.Constant;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;

//使用无权限信息的ZooKeeper会话访问含权限信息的数据节点
public class AuthSample_Get {

    final static String PATH = "/zk-book-auth_test";

    /**
     * 由于 zk2 没有带权限信息，导致返回了 NoAuth 的验证码；
     * 说明：一旦我们对一个数据节点设置了权限信息，那么其他没有权限设置的客户端会话将无法访问该数据节点，zk 服务端能为我们实现权限控制。
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {

        ZooKeeper zookeeper1 = new ZooKeeper(Constant.CONN_STR, 5000, null);
        zookeeper1.addAuthInfo("digest", "foo:true".getBytes());

        zookeeper1.create(PATH, "init".getBytes(), Ids.CREATOR_ALL_ACL, CreateMode.EPHEMERAL);

        ZooKeeper zookeeper2 = new ZooKeeper(Constant.CONN_STR, 50000, null);
        //返回了：KeeperErrorCode = NoAuth for /zk-book-auth_test
        zookeeper2.getData(PATH, false, null);
    }
}